<script setup lang="ts">
import { onMounted, ref } from 'vue'
import { useMainLogin } from '@/store/user'
import POISearch from '@components/POISearch/index.vue'

// 声明全局类型
declare global {
  interface Window {
    map: any;
  }
}

const user = useMainLogin()

onMounted(() => {
  document.addEventListener('fullscreenchange', () => {
    if (!document.fullscreenElement) {
      // 当用户通过其他方式（如按ESC）退出全屏时，也需要还原状态
      const marsContainer = document.querySelector('.mars3d-container') as HTMLElement
      const roadInfoBox = document.querySelector('.roadInfoBox') as HTMLElement
      const polyMainInfoBox = document.querySelector('.polyMainInfoBox') as HTMLElement
      
      if (marsContainer) {
        marsContainer.style.width = ''
        marsContainer.style.height = ''
      }
      if (roadInfoBox) roadInfoBox.style.display = ''
      if (polyMainInfoBox) polyMainInfoBox.style.display = ''
    }
  })
})

const flyTo = () => {
  if (user.position.length > 2) {
    let extent = {
      xmin: user.position[0],
      xmax: user.position[1],
      ymin: user.position[2],
      ymax: user.position[3],
    }
    window.map.flyToExtent(extent)
  } else {
    window.map.flyToPoint(user.position,{
      radius: user.height,
    })
  }
}

const preview = () => {
  const marsContainer = document.querySelector('.mars3d-container') as HTMLElement
  const roadInfoBox = document.querySelector('.roadInfoBox') as HTMLElement
  const polyMainInfoBox = document.querySelector('.polyMainInfoBox') as HTMLElement
  
  if (marsContainer) {
    if (document.fullscreenElement) {
      // 退出全屏
      document.exitFullscreen()
      if (roadInfoBox) roadInfoBox.style.display = ''
      if (polyMainInfoBox) polyMainInfoBox.style.display = ''
    } else {
      // 进入全屏
      marsContainer.requestFullscreen()
      if (roadInfoBox) roadInfoBox.style.display = 'none'
      if (polyMainInfoBox) polyMainInfoBox.style.display = 'none'
    }
  }
}
</script>

<template>
  <div class="header">
      <div class="lefttitle">

      </div>
      <div class="right">
        <div class="TopBtnBox">
          <POISearch></POISearch>
          <!-- 切换视角 -->
          <span class="TopBtn button" @click="flyTo">
            <img src="@/assets/image/home.png" alt="">
            <span class="tooltip"><div>飞至初始视角</div></span>
          </span>

          <span class="TopBtn button" @click="preview">
            <img src="@/assets/image/yanjing.png" alt="">
            <span class="tooltip"><div>全屏预览</div></span>
          </span>
          <span style="margin-right: 150px;"></span>
        </div>
      </div>
  </div>
</template>

<style scoped>
.header {
  --header-height: 60px;
  --header-width: calc(100vw - (var(--corner_box_padding) * -2) - 30px);
  --header-width-left: 710px;
  --header-width-middle: calc(var(--header-width) - var(--header-width-left) - var(--header-width-left));
  --header-font-color: #333;
  display: flex;
  align-items: center;
  justify-content: space-between;
  position: fixed;
  top: 0;
  width: 100%;
  height: var(--header-height);
  user-select: none;
}
.lefttitle {
  display: flex;
  justify-content: left;
  align-items: start;
  font-size: 22px;
  font-weight: 600;
}
.TopBtnBox {
  display: flex;
  align-items: center;
}
.TopBtnBox .TopBtn {
  box-sizing: border-box;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 50px;
  height: 50px;
  border-radius: 50%;
  cursor: pointer;
  background-color: rgba(29, 30, 34,.8);
  margin-right: 20px;
}
.TopBtnBox .TopBtn img {
  width: 28px;
  height: 28px;
}
.TopBtnBox .TopBtn .iconfont {
  font-size: 24px;
}
.TopBtnBox .TopBtn .iconfont path {
  fill: var(--header-font-color);
}
.TopBtnBox .TopBtn:hover .iconfont {
  animation: bellRing 0.9s both;
}

/* bell ringing animation keyframes*/
@keyframes bellRing {
  0%,
  100% {
    transform-origin: top;
  }

  15% {
    transform: rotateZ(10deg);
  }

  30% {
    transform: rotateZ(-10deg);
  }

  45% {
    transform: rotateZ(5deg);
  }

  60% {
    transform: rotateZ(-5deg);
  }

  75% {
    transform: rotateZ(2deg);
  }
}
.TopBtnBox .TopBtn.button.scale_9 {
  transform: scale(0.9);
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.3);
  color: #fff;
}
.TopBtnBox .TopBtn.button:active {
  transform: scale(0.8);
}

.TopBtnBox .TopBtn.button-bgColor {
  background-color: #3DBDA7;
}

.tooltip {
  position: absolute;
  top: 0;
  left: 50%;
  transform: translateX(-50%);
  font-size: 14px;
  color: var(--header-font-color);
  border-radius: 5px;
  box-shadow: 0 10px 10px rgba(0, 0, 0, 0.1);
  opacity: 0;
  pointer-events: none;
  transition: all 0.3s cubic-bezier(0.68, -0.55, 0.265, 1.55);
  width: 130px;
  padding: 5px 0;
  background: rgba(255, 255, 255,.2);
  border: 2px solid rgba(255, 255, 255,.5);
  text-align: center;
}

.tooltip::before {
  position: absolute;
  content: "";
  height: 8px;
  width: 8px;
  top: -3px;
  left: 50%;
  transform: translate(-50%) rotate(45deg);
  transition: all 0.3s cubic-bezier(0.68, -0.55, 0.265, 1.55);
  background: rgba(255, 255, 255, 0.4);
}

.TopBtn:hover .tooltip,
.tooltip:hover {
  top: 50px;
  opacity: 1;
  visibility: visible;
  pointer-events: auto;
}

.TopBtn:hover .tooltip,
.TopBtn:hover .tooltip::before {
  color: var(--header-font-color);
}
.tooltip_box-header:hover {
  background: rgba(0, 0, 0,.4);
  color: #ccc;
}
.path {
    stroke: var(--header-font-color);
}
</style>
<style>
.el-notification {
  background: rgba(0, 0, 0, .8);
}
/* 通知弹窗样式 */
.el-notification__title ,
.el-notification__content ,
.el-notification .el-notification__closeBtn {
  color: #fff;
}

</style>
